<HTML>
<HEAD>
<!-- This HTML file has been created by texi2html 1.45
     from schintro.txi on 19 Febuary 1997 -->

<TITLE>An Introduction to Scheme and its Implementation - and and or</TITLE>
</HEAD>
<BODY>
Go to the <A HREF="schintro_1.html">first</A>, <A HREF="schintro_21.html">previous</A>, <A HREF="schintro_23.html">next</A>, <A HREF="schintro_143.html">last</A> section, <A HREF="schintro_toc.html">table of contents</A>.
<HR>


<H4><A NAME="SEC22" HREF="schintro_toc.html#SEC22"><CODE>and</CODE> and <CODE>or</CODE></A></H4>

<P>
The special forms <CODE>and</CODE> and <CODE>or</CODE> can be used as logical operators,
but they can also be used as control structures, which is why they are
special forms.

</P>
<P>
<CODE>and</CODE> takes any number of expressions, and evaluates them in
sequence, until one of them returns <CODE>#f</CODE> or all of them
have been evaluated.  At the point where one returns <CODE>#f</CODE>, <CODE>and</CODE>
returns that value as the value of the <CODE>and</CODE> expression.  If none of
them returns <CODE>#f</CODE>, it returns the value of the last subexpression.

</P>
<P>
This is really a control construct, not just a logical operator, because
whether subexpressions get evaluated depends on the reults of the previous
subexpressions.

</P>
<P>
<CODE>and</CODE> is often used to express both control flow and value returning,
like a sequence of <CODE>if</CODE> tests.  You can write something like

</P>

<PRE>
(and (try-first-thing)
     (try-second-thing)
     (try-third-thing))
</PRE>

<P>
If the three calls all return true values, <CODE>and</CODE> returns the value
of the last one.  If any of them returns <CODE>#f</CODE>, however, none of
the rest are evaluated, and <CODE>#f</CODE> is returned as the value of the
overall expression.

</P>
<P>
Likewise, <CODE>or</CODE> takes any number of arguments, and returns the value
of the first one that returns a true value (i.e., anything but <CODE>#f</CODE>).
It stops when it gets a true value, and returns it without evaluating
the remaining subexpressions.

</P>

<PRE>
(or (try-first-thing)
    (try-second-thing)
    (try-third-thing))
</PRE>

<P>
<CODE>or</CODE> keeps trying subexpressions until one of them does return
a true value;  if that happens, <CODE>or</CODE> stops and returns that value.
If none of them returns anything but <CODE>#f</CODE>, it returns <CODE>#f</CODE>.

</P>


<H4><A NAME="SEC23" HREF="schintro_toc.html#SEC23"><CODE>not</CODE> is just a procedure</A></H4>

<P>
<CODE>not</CODE> is a procedure that takes one argument, which may be
any kind of Scheme value, and returns <CODE>#t</CODE> or <CODE>#f</CODE>.  If
the argument value is #f (the unique false object), it returns
<CODE>#t</CODE>, and otherwise returns <CODE>#f</CODE>.  That is, all values
count as true except for the false object--just as in a conditional.
For example, <CODE>(not 0)</CODE> returns <CODE>#f</CODE>.

</P>
<P>
Given that <CODE>and</CODE> and <CODE>or</CODE> are special forms, you might
think that the logical <CODE>not</CODE> operator is a special form
as well.  It isn't.  It's just a procedure--in particular, a
predicate.

</P>
<P>
This makes sense because <CODE>not</CODE> always evaluates its (one) argument,
and returns a value.  It doesn't treat any arguments specially--it's
just a normal first-class procedure, whose argument is evaluated
in the usual way before the procedure is actually called.

</P>
<P>
In general, operations that can be procedures <EM>are</EM> procedures.
Scheme only has special forms for things that are actually special,
and need their arguments treated differently from arguments to
procedure calls.  (Even Scheme's most powerful control construct,
<CODE>call-with-current-continuation</CODE>, is just a first-class
procedure.)

</P>

<PRE>
==================================================================
This is the end of Hunk A.

TIME TO TRY IT OUT

At this point, you should go read Hunk B of the next chapter
and work through the examples using a running Scheme system.
Then return here and resume this chapter.
==================================================================
</PRE>

<P>
(Go to Hunk B, which starts at section <A HREF="schintro_80.html#SEC86">An Interactive Programming Environment  (Hunk B)</A>.)

</P>
<HR>
Go to the <A HREF="schintro_1.html">first</A>, <A HREF="schintro_21.html">previous</A>, <A HREF="schintro_23.html">next</A>, <A HREF="schintro_143.html">last</A> section, <A HREF="schintro_toc.html">table of contents</A>.
</BODY>
</HTML>
